package util;

import java.io.InputStream;
import java.util.Properties;

import service.MainService;

import dao.UserDAO;

public class ObjectFactory {
	public static <T> T getInstance(Class<T> c){
		if(!c.isInterface()){
			throw new IllegalArgumentException("必须传入DAO接口的类对象");
		}
		try{
			String s = c.getSimpleName();
			InputStream is = ObjectFactory.class
					.getResourceAsStream("/objects.properties");
			System.out.println(is);
			Properties pro = new Properties();
			pro.load(is);
			is.close();
			String className = pro.getProperty(s);
			System.out.println(className);
			return (T)Class.forName(className).newInstance();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public static void main(String[] args){
		UserDAO dao = getInstance(UserDAO.class);
		System.out.println(dao.getClass());
		//System.out.println(dao);
		MainService service = getInstance(MainService.class);
		System.out.println(service);
	}
}
